package com.wswy.wzcx.api;

import android.util.Log;
import com.blankj.utilcode.util.EncryptUtils;
import com.blankj.utilcode.util.Utils;
import com.che.libcommon.utils.JsonKit;
import com.google.gson.reflect.TypeToken;
import com.wswy.wzcx.model.TrafficViolationInfo;
import com.wswy.wzcx.model.resp.TrafficViolationResp;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import kotlin.random.Random;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.Protocol;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okio.Okio;


class MockDataInterceptor implements Interceptor {

  private static final String TAG = "MockDataInterceptor";

  private static final MediaType CONTENT_TYPE =
      MediaType.parse("application/json; charset=utf-8");

  private Map<String,String> mockPathMap = new HashMap<>();
  private Map<String,String> caches = new HashMap<>();


  MockDataInterceptor() {
    mockPathMap.put("/v1/car/violation-query","mock/v111.json");
  }

  @Override
  public Response intercept(Chain chain) throws IOException {

    String path = chain.request().url().uri().getPath();

    String assetMockJson = mockPathMap.get(path);
    if (assetMockJson != null) {
      String jsonData = caches.get(path);
      if (jsonData == null) {
        jsonData = readText(assetMockJson);
        caches.put(path, jsonData);
      }

      ApiResult<TrafficViolationResp> json = JsonKit.GSON.fromJson(jsonData, new TypeToken<ApiResult<TrafficViolationResp>>() {
      }.getType());


      int tt = 0;
      if (Random.Default.nextBoolean()){

        tt = Random.Default.nextInt(5);

        for (int i = 0; i < tt; i++) {

          TrafficViolationInfo fakeInfo = new TrafficViolationInfo();
          fakeInfo.id = 10000000 + Random.Default.nextLong(10,1000000);
          fakeInfo.carNo = "苏A91GP8";
          fakeInfo.fineTime = "2018-05-05 17:53:05";
          fakeInfo.fineLocation = "G18493公里--";
          fakeInfo.fineDetail = "驾驶中型以上载客载货汽车、危险物品运输车辆以外的机动车超过规定时速10%以下的---"+i;
          fakeInfo.fineDeductPoints = 0;
          fakeInfo.fineFee = "0";
          fakeInfo.lat = 36.66853;
          fakeInfo.lon = 117.020355;

          fakeInfo.fineCity = "上海";
          fakeInfo.province = "上海";
          fakeInfo.uniqueMd5Id = EncryptUtils.encryptMD5ToString(System.currentTimeMillis()+i+"----");
          fakeInfo.statusName = "20";

          json.data.fineData.getFineList().add(fakeInfo);
        }

      }

      Log.e(TAG, "intercept --> " + path+"   add:"+tt);
      return generatorSuccessResponse(chain.request(), JsonKit.GSON.toJson(json));
    }

    return chain.proceed(chain.request());
  }

  private String readText(String assetPath){
    try {
      InputStream open = Utils.getApp().getAssets().open(assetPath);
      return Okio.buffer(Okio.source(open)).readByteString().utf8();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return null;
  }

  private Response generatorSuccessResponse(Request request, String dataJson){
    return new Response.Builder().code(200).protocol(Protocol.HTTP_1_1).
        message("ok")
        .addHeader("Content-Type", "application/json;charset=UTF-8")
        .request(request)
        .body(ResponseBody.create(CONTENT_TYPE, dataJson)).build();
  }


}
